Repository navigation
Fix seven runtime problems found in stock hxcpp's recent reports - #499
Merged
Merged
Conversation
When the moving collector moves an object that has an id, MoveSpecial found its entry in sObjectIdMap, inserted the new address with operator[], and then erased the old entry through the iterator it had found. The insert can rehash the unordered_map, and a rehash invalidates every iterator, so the erase was undefined behaviour. Any id'd object can be moved, ObjectMap keys among them, since under HXCPP_GC_MOVING they all take their hash from __hxcpp_obj_id. The id is now read out, the old entry erased, and the new one inserted, as the finalizer and zombie maps just above already do. No test can fail before this: libstdc++, libc++ and MSVC all keep a node's iterator valid through a rehash, even in their debug modes. Issue679 moves 20,000 ObjectMap keys through compactions and still finds every one. HaxeFoundation/hxcpp#1468 Tests: Issue679
host_to_string_ipv6, socket_connect_ipv6 and socket_bind_ipv6 read 16 bytes from &host[0] whatever the array's length. An empty array grew to one element and was read 15 bytes past it, a shorter one was read past its end, and a null one crashed. So host_to_string_ipv6 of four 0xff bytes gave "ffff:ffff::106:8010", and connect and bind went to whatever address followed the array in memory. host_reverse_ipv6 already checked for 16 bytes and copied them out first. The four functions share one helper now, which copies the address out of the array and refuses a null array or one of any length but 16. host_to_string_ipv6 gives null for such an array, as host_reverse_ipv6 does, and connect and bind throw "Invalid IPv6 address". sys.net.Host always passes the 16 bytes host_resolve_ipv6 made, so only direct cpp.NativeSocket callers could reach this. HaxeFoundation/hxcpp@a8a5724f, which swapped &a[0] for Pointer() in these functions, does not check the length either. Tests: SocketIpv6AddressLength
TVars reads its declaration count from the file, and checkCount accepts 0. runVoid then started from &vars[0] on the empty vector, which is undefined behaviour: a standard library that checks indexing, such as libstdc++ with _GLIBCXX_ASSERTIONS or MSVC's checked iterators, stops the program there. The Haxe compiler never writes an empty one, so only a malformed or hand-made file reaches it. BlockExpr guards the same pattern with a size check. runVoid walks the declarations by index now, as genCode already did. CppiaEmptyVars loads hand-written modules whose main is an empty var statement, alone and twice in a block, and runs them interpreted and under the JIT. No build here checks indexing, so it passed before too. HaxeFoundation/hxcpp@a8a5724f Tests: CppiaEmptyVars
libhxcpp #455 made Haxe 4's haxe.atomic.AtomicInt operations safe points, through cpp::Pointer<int> overloads of the _hx_atomic_* functions. Haxe 5.0.0-preview.1's AtomicInt is a class with an Int field instead, and calls _hx_atomic_add(&{0}, ...) on that field, so it passes a plain int *. That binds to the runtime's volatile int * forms, which never stop, so a thread spinning on flag.load() still held up every collection, and every other thread with it. AtomicBool wraps AtomicInt and had the same gap. AtomicSpinSafePoint has failed this way on the preview.1 jobs, where failures are warnings. The stopping forms take an int * now, and the cpp::Pointer<int> ones call them. The runtime's own spinlocks pass a volatile int *, so they keep the forms that do not stop. _hx_atomic_xor was the one runtime form that took an int *; it takes a volatile int * like the others. AtomicFieldSpinSafePoint does what preview.1's AtomicInt does on any Haxe: a thread spins on each of the nine operations in turn, called on the address of an Int field, while the main thread collects. Before this, the first collection never finished and its watchdog ended the test after 60 s; it passes in under a second now, and with HXCPP_GC_MOVING. HaxeFoundation/hxcpp#1443 Tests: AtomicFieldSpinSafePoint, AtomicSpinSafePoint
haxe.Int64.isInt64(100) was true on C++ and false on the other targets, and so was isInt64(1.0), because a whole Float boxes as an Int. Haxe 4.2 implements isInt64 with cpp::Int64Struct::is, which asks Int64Handler, and Haxe 4.3 and 5.0.0-preview.1 with Std.isOfType(val, cpp.Int64), which asks the cpp::Int64 class's IsInt64 in Dynamic.cpp. Both took a boxed Int as well as a boxed Int64. They took Ints because small Int64 values, -1 to 255, used to come out of the boxed Int cache. Since 6.0.0 they have a cache of their own, so every boxed Int64 is an Int64Data and reports vtInt64, and both checks now take only that. This changes behaviour for code that used isInt64 to accept any integer. Std.isOfType(int64, Int) stays true for a value that fits, as for a whole Float. Int64IsInt64 checks isInt64 on Ints, a whole Float, null, a String and Int64 values on both sides of the box cache, asks the handler and the class directly so each is covered whichever Haxe builds it, and asks again from a cppia script, interpreted and under the JIT. Before this, the Int, 0 and 1.0 cases were all true. HaxeFoundation/hxcpp#1461 Tests: Int64IsInt64
A thread in a GC-free zone that allocates, or collects, while another thread collects reaches PauseForCollect and waits there for the collection to finish. The collector neither waits for a thread in a zone nor releases it at the end, so in a release build that thread waited for ever, with nothing to say why. Native code that misses an ExitGCFreeZone is the usual way there. Debug builds report any allocation or collection in a zone, before it gets that far. PauseForCollect now ends the program with a critical error that names the misuse when its thread is in a zone. No correct caller gets there: safe points return first in a zone, as do the Mutex and Condition fast paths that call them, and ExitGCFreeZone waits out a collection on the state lock instead. GcFreeZoneAllocationReported runs the misuse in a child process: one thread enters a zone and allocates once a collection is pending, while another keeps it pending by reaching no safe point for half a second. Before this the allocation never returned and the child said so after 5 s; now the child prints the error and ends, and a debug build's own message is accepted too. HaxeFoundation/hxcpp#1468 Tests: GcFreeZoneAllocationReported
mbedtls_x509_crt_parse_file and mbedtls_x509_crt_parse_path return a negative error code, or, when they parsed something, a positive count of the certificates they could not parse. cert_load_file and cert_load_path passed any non-zero result to ssl_error, and mbedtls_strerror negates a positive value and looks it up: one bad certificate threw "ERROR - Generic error", two "BIGNUM - An error occurred while reading from or writing to a file", and so on. A negative result still goes to ssl_error. A positive one throws "Could not parse 1 certificate in <file>", or "certificates" for more. For a directory the message says "certificates or files", because parse_path also counts each file it could not read a certificate from. Both still throw, as before; whether a directory or bundle that partly parsed should be kept instead is left for its own change. SslCertificateParseCount writes a good certificate, the same with one corrupt certificate after it, and one between two corrupt ones, loads each, and loads a directory with a certificate and a text file. Before this, the three messages were the mbedTLS ones. HaxeFoundation/hxcpp@45adede6 Tests: SslCertificateParseCount
HaxeFoundation/hxcpp#1047, to keep typed callables working at API level 500, stopped cppia linking a host class's static object fields to a direct reference: a script closure stored through an hx::Object pointer into a typed function slot is not callable as one. The exclusion had no API level guard, so in Haxe 4 builds, and Haxe 5.0.0-preview.1 ones, which build against the 430 runtime, every script read or write of a host static object, a singleton, a static Array or a static function, was a __GetStatic or __SetStatic by name, interpreted and jitted. c653b13 restored direct access for object members the same way and left statics out. The exclusion applies at API level 500 only now, as for members, and below that an object static gets the same MemReference an Int or String static already had. Statics are roots, marked at the start of every collection, so the direct store needs no write barrier, as a compiled store into a static has none. A new host_statics workload in bench/cppia reads one host static Array and stores another on each pass. Against f0622380, with --base: interpreter 13.2M -> 23.4M iterations/s (14.3M -> 25.8M rerun) JIT 31.5M -> 386M iterations/s (35.6M -> 542M rerun) native_call, which this cannot reach, read 8% slower under the JIT in both runs (287M against 312M). Keeping only Script.main's store to CppiaBridge.workload on the named path brought it to 296M against 301M, within the spread: the JIT compiles lazily, so main's smaller code moved every function compiled after it. CppiaHostStaticObjects has a script store young objects, an Array and a closure into host statics that alone hold them, through minor and full collections with HXCPP_GC_GENERATIONAL, then read them back in the script and in the host, interpreted and under the JIT. It passed before too, through the named path. HaxeFoundation/hxcpp#1365 Tests: CppiaHostStaticObjects, CppiaObjectFields
dimensionscape
force-pushed
the
fix/upstream-survey-runtime
branch
from
November 27, 2026 22:05
5aab327 to
c8077d2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Seven runtime problems found while checking stock hxcpp's commits, pull requests and issues since the fork point against libhxcpp. Each fix is its own commit with a regression test and a Changes.md line. The Changes.md lines go under 6.0.3.
int *stop for a collection. Haxe 5.0.0-preview.1's AtomicInt passes the address of an Int field to the_hx_atomic_*functions, which chose the runtime'svolatile int *forms that never stop, so a thread spinning onflag.load()held up every collection. This is the gap AtomicSpinSafePoint has shown on the preview.1 jobs, where failures are warnings.AtomicFieldSpinSafePointdoes the same on any Haxe and hung until its watchdog before the fix. (Don't enter GC free zones in atomic operations HaxeFoundation/hxcpp#1443)MoveSpecialerased through an iterator that the insert before it could invalidate by rehashing. No test can fail before this, since libstdc++, libc++ and MSVC keep the iterator through a rehash; Issue679 covers the path. (GC stability fixes: moving-GC compaction overrun, thread registration races, verify wrap, id map HaxeFoundation/hxcpp#1468)host_to_string_ipv6,socket_connect_ipv6andsocket_bind_ipv6read 16 bytes whatever the array's length. They now sharehost_reverse_ipv6's check: null fromhost_to_string_ipv6, and "Invalid IPv6 address" from connect and bind. Tested bySocketIpv6AddressLength. (HaxeFoundation/hxcpp@a8a5724f)&vars[0]of an empty vector.CppiaEmptyVarscovers it, though no build here checks indexing, so it passed before too. (HaxeFoundation/hxcpp@a8a5724f)haxe.Int64.isInt64is false for an Int, as on the other targets. This changes behaviour, and the Changes.md line says so. Haxe 4.2 asksInt64Struct::isand Haxe 4.3 and laterStd.isOfType(v, cpp.Int64), and both took a boxed Int, soisInt64(100)andisInt64(1.0)were true. Tested byInt64IsInt64, compiled and from cppia. (isInt64 is true for Int value on cpp HaxeFoundation/hxcpp#1461)GcFreeZoneAllocationReportedruns it in a child process; before the fix the allocation never returned. (GC stability fixes: moving-GC compaction overrun, thread registration races, verify wrap, id map HaxeFoundation/hxcpp#1468)Certificate.loadFileandloadPathnow say how many. Tested bySslCertificateParseCount. (HaxeFoundation/hxcpp@45adede6)host_staticsworkload in bench/cppia goes from 13.2M to 23.4M iterations/s interpreted, and from 31.5M to 386M under the JIT.CppiaHostStaticObjectschecks the stores through minor and full collections. (Typed callables vs Cppia MemReference HaxeFoundation/hxcpp#1365)On Haxe 4.3.7 on Windows, all 372 regression tests pass, as do test/haxe and test/std. Haxe 5.0.0-preview.1, Linux, macOS and Emscripten are left to CI.
Not changed here: whether
loadFileandloadPathshould keep the certificates that parsed when some do not, instead of throwing.